//实时监听鼠标位置
var moveX = 0;
var moveY = 0;

//选中的div的初始位置
function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    moveX = event.offsetX;
    moveY = event.offsetY;
    ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    // console.log(data);
    var clone = document.getElementById(data);
    ev.target.appendChild(clone);
    var page = ev.target.offsetParent.id;
    // console.log(page);
    // 父级的l，t
    clone.style.position = "absolute";
    clone.style.left = ev.offsetX - moveX + "px";
    clone.style.top = ev.offsetY - moveY + "px";
    console.log({'id': clone.id, 'x': ev.offsetX - moveX, 'y': ev.offsetY - moveY, 'page': page});
    window.data[clone.id] = {'x': ev.offsetX - moveX, 'y': ev.offsetY - moveY, 'page': clone.parentElement.id};
    return false;
}

$('.dosubmit').click(function (e) {
    e.stopPropagation();
    e.preventDefault();
    var datas = localStorage.getItem('data'), dataJson = JSON.parse(datas);
    window.data['title'] = dataJson.title;
    window.data['project_id'] = dataJson.project_id;
    window.data['company_id'] = dataJson.company_id;
    window.data['expiretime'] = dataJson.expiretime;
    //ajax 合同保存
    $.ajax({
        url: 'crecom',
        data: {data: window.data},
        type: "POST",
        dataType: "json",
        success: function (result) {
            console.log(result);
            if (result.status == 1) {
                $.amaran({'message': result.info})
                window.location.href = result.url;
            }
        },
        error: function (result, xhr, status, errorThrown) {
            $.amaran({'message': result.info})
        },
        complete: function (xhr, status) {
//                    alert('The request is complated');
        },
        beforesend: function () {
        },
        cache: false,//true
        async: true
    });//ajax end here
    console.log(window.data);
})